<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>正则的扩展 | Evan&#39;s blog</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/blog/img/favicon.ico">
    <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/blog/assets/css/0.styles.5cae76fb.css" as="style"><link rel="preload" href="/blog/assets/js/app.aaf1b95a.js" as="script"><link rel="preload" href="/blog/assets/js/2.4e88da26.js" as="script"><link rel="preload" href="/blog/assets/js/89.a7a762a6.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.77cd5446.js"><link rel="prefetch" href="/blog/assets/js/100.a4b7c22a.js"><link rel="prefetch" href="/blog/assets/js/101.f10a486b.js"><link rel="prefetch" href="/blog/assets/js/102.c6fb27ad.js"><link rel="prefetch" href="/blog/assets/js/103.6544c57a.js"><link rel="prefetch" href="/blog/assets/js/104.6da84eb0.js"><link rel="prefetch" href="/blog/assets/js/105.e98afb5b.js"><link rel="prefetch" href="/blog/assets/js/106.00c6a618.js"><link rel="prefetch" href="/blog/assets/js/107.3e384ab5.js"><link rel="prefetch" href="/blog/assets/js/108.cc8462f3.js"><link rel="prefetch" href="/blog/assets/js/109.efac7118.js"><link rel="prefetch" href="/blog/assets/js/11.0b77905f.js"><link rel="prefetch" href="/blog/assets/js/110.bfd96a59.js"><link rel="prefetch" href="/blog/assets/js/111.d67fb86c.js"><link rel="prefetch" href="/blog/assets/js/112.9ecfc68a.js"><link rel="prefetch" href="/blog/assets/js/113.dd57f9de.js"><link rel="prefetch" href="/blog/assets/js/114.a12ac3b9.js"><link rel="prefetch" href="/blog/assets/js/115.856e382b.js"><link rel="prefetch" href="/blog/assets/js/116.941afac4.js"><link rel="prefetch" href="/blog/assets/js/117.df53da43.js"><link rel="prefetch" href="/blog/assets/js/118.906f3d41.js"><link rel="prefetch" href="/blog/assets/js/119.33bd4cde.js"><link rel="prefetch" href="/blog/assets/js/12.6a1ae269.js"><link rel="prefetch" href="/blog/assets/js/120.c7d90a2a.js"><link rel="prefetch" href="/blog/assets/js/121.e9a583b9.js"><link rel="prefetch" href="/blog/assets/js/122.d2edbcb6.js"><link rel="prefetch" href="/blog/assets/js/123.1a632657.js"><link rel="prefetch" href="/blog/assets/js/124.2a2decb0.js"><link rel="prefetch" href="/blog/assets/js/125.f9344214.js"><link rel="prefetch" href="/blog/assets/js/126.90a937ca.js"><link rel="prefetch" href="/blog/assets/js/127.541a2bd8.js"><link rel="prefetch" href="/blog/assets/js/128.d197e7a9.js"><link rel="prefetch" href="/blog/assets/js/129.bd2b74bf.js"><link rel="prefetch" href="/blog/assets/js/13.a3d3aa42.js"><link rel="prefetch" href="/blog/assets/js/130.4555d2a9.js"><link rel="prefetch" href="/blog/assets/js/131.af4d4381.js"><link rel="prefetch" href="/blog/assets/js/132.15b408e6.js"><link rel="prefetch" href="/blog/assets/js/133.c6819d56.js"><link rel="prefetch" href="/blog/assets/js/134.2b49eb48.js"><link rel="prefetch" href="/blog/assets/js/135.7b5224d1.js"><link rel="prefetch" href="/blog/assets/js/136.e966c07b.js"><link rel="prefetch" href="/blog/assets/js/137.962a7287.js"><link rel="prefetch" href="/blog/assets/js/138.e4446fa5.js"><link rel="prefetch" href="/blog/assets/js/139.d0a5415a.js"><link rel="prefetch" href="/blog/assets/js/14.d68bef9a.js"><link rel="prefetch" href="/blog/assets/js/140.23d79a44.js"><link rel="prefetch" href="/blog/assets/js/141.e09939d0.js"><link rel="prefetch" href="/blog/assets/js/142.266a27e9.js"><link rel="prefetch" href="/blog/assets/js/143.ed3c54b1.js"><link rel="prefetch" href="/blog/assets/js/144.98c6792d.js"><link rel="prefetch" href="/blog/assets/js/145.a18f3f3f.js"><link rel="prefetch" href="/blog/assets/js/146.106bb541.js"><link rel="prefetch" href="/blog/assets/js/147.4b313651.js"><link rel="prefetch" href="/blog/assets/js/148.4a6ec42d.js"><link rel="prefetch" href="/blog/assets/js/149.9ec297ab.js"><link rel="prefetch" href="/blog/assets/js/15.81a99c55.js"><link rel="prefetch" href="/blog/assets/js/150.823ccad7.js"><link rel="prefetch" href="/blog/assets/js/151.1eee0fd4.js"><link rel="prefetch" href="/blog/assets/js/152.71ae0047.js"><link rel="prefetch" href="/blog/assets/js/153.63d5a471.js"><link rel="prefetch" href="/blog/assets/js/154.77169175.js"><link rel="prefetch" href="/blog/assets/js/155.c460c129.js"><link rel="prefetch" href="/blog/assets/js/156.793e4b57.js"><link rel="prefetch" href="/blog/assets/js/157.c9261336.js"><link rel="prefetch" href="/blog/assets/js/158.f2160023.js"><link rel="prefetch" href="/blog/assets/js/159.33ddd8b3.js"><link rel="prefetch" href="/blog/assets/js/16.6a1f4400.js"><link rel="prefetch" href="/blog/assets/js/160.cd0b03b9.js"><link rel="prefetch" href="/blog/assets/js/161.d4b3bb73.js"><link rel="prefetch" href="/blog/assets/js/162.c69e83b1.js"><link rel="prefetch" href="/blog/assets/js/163.9c1b18fa.js"><link rel="prefetch" href="/blog/assets/js/164.854148c8.js"><link rel="prefetch" href="/blog/assets/js/165.aa0c4f4d.js"><link rel="prefetch" href="/blog/assets/js/166.54fc4b36.js"><link rel="prefetch" href="/blog/assets/js/167.ca2d6801.js"><link rel="prefetch" href="/blog/assets/js/168.cf378109.js"><link rel="prefetch" href="/blog/assets/js/169.a298949f.js"><link rel="prefetch" href="/blog/assets/js/17.ad83a175.js"><link rel="prefetch" href="/blog/assets/js/170.1f5f6b47.js"><link rel="prefetch" href="/blog/assets/js/171.aec76c3b.js"><link rel="prefetch" href="/blog/assets/js/172.be4a4852.js"><link rel="prefetch" href="/blog/assets/js/173.80a0f3b5.js"><link rel="prefetch" href="/blog/assets/js/174.e3a39617.js"><link rel="prefetch" href="/blog/assets/js/175.597dc79e.js"><link rel="prefetch" href="/blog/assets/js/176.29eb04a2.js"><link rel="prefetch" href="/blog/assets/js/177.e91f6f26.js"><link rel="prefetch" href="/blog/assets/js/178.34ce2871.js"><link rel="prefetch" href="/blog/assets/js/179.f2cef142.js"><link rel="prefetch" href="/blog/assets/js/18.11cfb1b5.js"><link rel="prefetch" href="/blog/assets/js/180.327428c1.js"><link rel="prefetch" href="/blog/assets/js/181.d6f70081.js"><link rel="prefetch" href="/blog/assets/js/182.e5313823.js"><link rel="prefetch" href="/blog/assets/js/183.d08dbc32.js"><link rel="prefetch" href="/blog/assets/js/184.ccfa8d5d.js"><link rel="prefetch" href="/blog/assets/js/185.9695e392.js"><link rel="prefetch" href="/blog/assets/js/186.3a1b272d.js"><link rel="prefetch" href="/blog/assets/js/187.e11331a6.js"><link rel="prefetch" href="/blog/assets/js/188.2a3eab47.js"><link rel="prefetch" href="/blog/assets/js/189.71a6890e.js"><link rel="prefetch" href="/blog/assets/js/19.1cca0d39.js"><link rel="prefetch" href="/blog/assets/js/190.41f05291.js"><link rel="prefetch" href="/blog/assets/js/191.6db1a29f.js"><link rel="prefetch" href="/blog/assets/js/192.6d6a9425.js"><link rel="prefetch" href="/blog/assets/js/193.1ac4e1e9.js"><link rel="prefetch" href="/blog/assets/js/194.e3056d02.js"><link rel="prefetch" href="/blog/assets/js/195.937d6e29.js"><link rel="prefetch" href="/blog/assets/js/196.775b3da3.js"><link rel="prefetch" href="/blog/assets/js/197.437fe16f.js"><link rel="prefetch" href="/blog/assets/js/198.e02c3fd3.js"><link rel="prefetch" href="/blog/assets/js/199.da036572.js"><link rel="prefetch" href="/blog/assets/js/20.f58b2515.js"><link rel="prefetch" href="/blog/assets/js/200.d70ebfc1.js"><link rel="prefetch" href="/blog/assets/js/201.5a0cc6a8.js"><link rel="prefetch" href="/blog/assets/js/202.b6f4dfe9.js"><link rel="prefetch" href="/blog/assets/js/203.ac5f2eea.js"><link rel="prefetch" href="/blog/assets/js/204.e4197e35.js"><link rel="prefetch" href="/blog/assets/js/205.e988b5e5.js"><link rel="prefetch" href="/blog/assets/js/206.7d6d7ee2.js"><link rel="prefetch" href="/blog/assets/js/207.e9476276.js"><link rel="prefetch" href="/blog/assets/js/208.90d387ec.js"><link rel="prefetch" href="/blog/assets/js/209.01278130.js"><link rel="prefetch" href="/blog/assets/js/21.f2eb7cbe.js"><link rel="prefetch" href="/blog/assets/js/210.08914606.js"><link rel="prefetch" href="/blog/assets/js/211.b5ae3862.js"><link rel="prefetch" href="/blog/assets/js/212.03a9909c.js"><link rel="prefetch" href="/blog/assets/js/213.88999a25.js"><link rel="prefetch" href="/blog/assets/js/214.5e6517ae.js"><link rel="prefetch" href="/blog/assets/js/215.7c05faa3.js"><link rel="prefetch" href="/blog/assets/js/216.0c0d0250.js"><link rel="prefetch" href="/blog/assets/js/217.3e6a1882.js"><link rel="prefetch" href="/blog/assets/js/218.7bdaa47b.js"><link rel="prefetch" href="/blog/assets/js/219.4a9b382a.js"><link rel="prefetch" href="/blog/assets/js/22.0225f4f3.js"><link rel="prefetch" href="/blog/assets/js/220.da13afe0.js"><link rel="prefetch" href="/blog/assets/js/221.b93baddd.js"><link rel="prefetch" href="/blog/assets/js/222.6f88fa87.js"><link rel="prefetch" href="/blog/assets/js/223.23532ff7.js"><link rel="prefetch" href="/blog/assets/js/224.bba40504.js"><link rel="prefetch" href="/blog/assets/js/225.e87911f0.js"><link rel="prefetch" href="/blog/assets/js/226.27fcd8f6.js"><link rel="prefetch" href="/blog/assets/js/227.7075a8c3.js"><link rel="prefetch" href="/blog/assets/js/228.d5363587.js"><link rel="prefetch" href="/blog/assets/js/229.95d21664.js"><link rel="prefetch" href="/blog/assets/js/23.203b3a9e.js"><link rel="prefetch" href="/blog/assets/js/230.eee47d34.js"><link rel="prefetch" href="/blog/assets/js/231.686012e1.js"><link rel="prefetch" href="/blog/assets/js/232.d780f01b.js"><link rel="prefetch" href="/blog/assets/js/233.fba323db.js"><link rel="prefetch" href="/blog/assets/js/234.7ac11688.js"><link rel="prefetch" href="/blog/assets/js/235.61ba2d32.js"><link rel="prefetch" href="/blog/assets/js/24.26ad942d.js"><link rel="prefetch" href="/blog/assets/js/25.e9864d4c.js"><link rel="prefetch" href="/blog/assets/js/26.1a4e2ed9.js"><link rel="prefetch" href="/blog/assets/js/27.f56ff53e.js"><link rel="prefetch" href="/blog/assets/js/28.3c5bd15e.js"><link rel="prefetch" href="/blog/assets/js/29.0104f169.js"><link rel="prefetch" href="/blog/assets/js/3.3ec6f00b.js"><link rel="prefetch" href="/blog/assets/js/30.9aaf650a.js"><link rel="prefetch" href="/blog/assets/js/31.50bfe700.js"><link rel="prefetch" href="/blog/assets/js/32.33630021.js"><link rel="prefetch" href="/blog/assets/js/33.703693c2.js"><link rel="prefetch" href="/blog/assets/js/34.51cfd584.js"><link rel="prefetch" href="/blog/assets/js/35.5614a593.js"><link rel="prefetch" href="/blog/assets/js/36.3f9fb418.js"><link rel="prefetch" href="/blog/assets/js/37.d3471a6e.js"><link rel="prefetch" href="/blog/assets/js/38.303beb9a.js"><link rel="prefetch" href="/blog/assets/js/39.528e5c8c.js"><link rel="prefetch" href="/blog/assets/js/4.f7a68e82.js"><link rel="prefetch" href="/blog/assets/js/40.5d37d2ac.js"><link rel="prefetch" href="/blog/assets/js/41.5e434fb6.js"><link rel="prefetch" href="/blog/assets/js/42.58ebb1e1.js"><link rel="prefetch" href="/blog/assets/js/43.f7344b04.js"><link rel="prefetch" href="/blog/assets/js/44.ba8e2c32.js"><link rel="prefetch" href="/blog/assets/js/45.b59d07a4.js"><link rel="prefetch" href="/blog/assets/js/46.cf56a467.js"><link rel="prefetch" href="/blog/assets/js/47.2307b39c.js"><link rel="prefetch" href="/blog/assets/js/48.0f967628.js"><link rel="prefetch" href="/blog/assets/js/49.e9c76177.js"><link rel="prefetch" href="/blog/assets/js/5.43eb1773.js"><link rel="prefetch" href="/blog/assets/js/50.ef1c92fd.js"><link rel="prefetch" href="/blog/assets/js/51.472db098.js"><link rel="prefetch" href="/blog/assets/js/52.b151aea0.js"><link rel="prefetch" href="/blog/assets/js/53.0df4c526.js"><link rel="prefetch" href="/blog/assets/js/54.352990e8.js"><link rel="prefetch" href="/blog/assets/js/55.27accd61.js"><link rel="prefetch" href="/blog/assets/js/56.91d392d0.js"><link rel="prefetch" href="/blog/assets/js/57.4f8d55c3.js"><link rel="prefetch" href="/blog/assets/js/58.aba5c165.js"><link rel="prefetch" href="/blog/assets/js/59.1f31aefa.js"><link rel="prefetch" href="/blog/assets/js/6.709e31d8.js"><link rel="prefetch" href="/blog/assets/js/60.c4c2ad7c.js"><link rel="prefetch" href="/blog/assets/js/61.84c44795.js"><link rel="prefetch" href="/blog/assets/js/62.847d5a79.js"><link rel="prefetch" href="/blog/assets/js/63.4526d774.js"><link rel="prefetch" href="/blog/assets/js/64.149c457f.js"><link rel="prefetch" href="/blog/assets/js/65.ffb7241e.js"><link rel="prefetch" href="/blog/assets/js/66.ab3a1fd7.js"><link rel="prefetch" href="/blog/assets/js/67.960d17a4.js"><link rel="prefetch" href="/blog/assets/js/68.1ecea3e5.js"><link rel="prefetch" href="/blog/assets/js/69.24a059f9.js"><link rel="prefetch" href="/blog/assets/js/7.3123b0d0.js"><link rel="prefetch" href="/blog/assets/js/70.087b119f.js"><link rel="prefetch" href="/blog/assets/js/71.36ec64ad.js"><link rel="prefetch" href="/blog/assets/js/72.72180002.js"><link rel="prefetch" href="/blog/assets/js/73.76a3edbc.js"><link rel="prefetch" href="/blog/assets/js/74.fb379a54.js"><link rel="prefetch" href="/blog/assets/js/75.eaa559bd.js"><link rel="prefetch" href="/blog/assets/js/76.156bd9c3.js"><link rel="prefetch" href="/blog/assets/js/77.123a161c.js"><link rel="prefetch" href="/blog/assets/js/78.19a324be.js"><link rel="prefetch" href="/blog/assets/js/79.e24877d1.js"><link rel="prefetch" href="/blog/assets/js/8.5c81f7fc.js"><link rel="prefetch" href="/blog/assets/js/80.22910223.js"><link rel="prefetch" href="/blog/assets/js/81.169a78ab.js"><link rel="prefetch" href="/blog/assets/js/82.c300aae5.js"><link rel="prefetch" href="/blog/assets/js/83.fc555e72.js"><link rel="prefetch" href="/blog/assets/js/84.8e8a9c99.js"><link rel="prefetch" href="/blog/assets/js/85.7c060e20.js"><link rel="prefetch" href="/blog/assets/js/86.946a80b1.js"><link rel="prefetch" href="/blog/assets/js/87.8e5e2c47.js"><link rel="prefetch" href="/blog/assets/js/88.d2f97880.js"><link rel="prefetch" href="/blog/assets/js/9.dde6002e.js"><link rel="prefetch" href="/blog/assets/js/90.c6447971.js"><link rel="prefetch" href="/blog/assets/js/91.403877ec.js"><link rel="prefetch" href="/blog/assets/js/92.44bb8649.js"><link rel="prefetch" href="/blog/assets/js/93.b9f553db.js"><link rel="prefetch" href="/blog/assets/js/94.27628f49.js"><link rel="prefetch" href="/blog/assets/js/95.7ad3dc73.js"><link rel="prefetch" href="/blog/assets/js/96.adc8f3f6.js"><link rel="prefetch" href="/blog/assets/js/97.f5b76103.js"><link rel="prefetch" href="/blog/assets/js/98.e8878d44.js"><link rel="prefetch" href="/blog/assets/js/99.037be397.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.5cae76fb.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="Evan's blog" class="logo"> <span class="site-name can-hide">Evan's blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/xugaoyi/image_store/blog/20200103123203.jpg"> <div class="blogger-info"><h3>Evan Xu</h3> <span>前端界的小学生</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/blog/pages/f344d070a1031ef7/" class="sidebar-link">ECMAScript 6 简介</a></li><li><a href="/blog/pages/c1edd70a6b7c7872/" class="sidebar-link">let 和 const 命令</a></li><li><a href="/blog/pages/b1ab10a62f7564da/" class="sidebar-link">变量的解构赋值</a></li><li><a href="/blog/pages/ca89eca8adeba5f4/" class="sidebar-link">字符串的扩展</a></li><li><a href="/blog/pages/a650b4a0ebfc9350/" class="sidebar-link">字符串的新增方法</a></li><li><a href="/blog/pages/0473261a6ab0ee8c/" aria-current="page" class="active sidebar-link">正则的扩展</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-构造函数" class="sidebar-link">RegExp 构造函数</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#字符串的正则方法" class="sidebar-link">字符串的正则方法</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#u-修饰符" class="sidebar-link">u 修饰符</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-unicode-属性" class="sidebar-link">RegExp.prototype.unicode 属性</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#y-修饰符" class="sidebar-link">y 修饰符</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-sticky-属性" class="sidebar-link">RegExp.prototype.sticky 属性</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-flags-属性" class="sidebar-link">RegExp.prototype.flags 属性</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#s-修饰符-dotall-模式" class="sidebar-link">s 修饰符：dotAll 模式</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#后行断言" class="sidebar-link">后行断言</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#unicode-属性类" class="sidebar-link">Unicode 属性类</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#具名组匹配" class="sidebar-link">具名组匹配</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#简介" class="sidebar-link">简介</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#解构赋值和替换" class="sidebar-link">解构赋值和替换</a></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#引用" class="sidebar-link">引用</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/pages/0473261a6ab0ee8c/#string-prototype-matchall" class="sidebar-link">String.prototype.matchAll()</a></li></ul></li><li><a href="/blog/pages/5dfea9a0f2d1a392/" class="sidebar-link">数值的扩展</a></li><li><a href="/blog/pages/8ed309d668b20264/" class="sidebar-link">函数的扩展</a></li><li><a href="/blog/pages/e34009d60d8bc4b2/" class="sidebar-link">数组的扩展</a></li><li><a href="/blog/pages/b5e3e0a0ff6e9c25/" class="sidebar-link">对象的扩展</a></li><li><a href="/blog/pages/e85e68947502cf90/" class="sidebar-link">对象的新增方法</a></li><li><a href="/blog/pages/02c86eb2792f3262/" class="sidebar-link">Symbol</a></li><li><a href="/blog/pages/0c21dae358fca16b/" class="sidebar-link">Set 和 Map 数据结构</a></li><li><a href="/blog/pages/f56ec2ab97d60483/" class="sidebar-link">Proxy</a></li><li><a href="/blog/pages/74de3e45e4491e95/" class="sidebar-link">Reflect</a></li><li><a href="/blog/pages/2810ae8985e9bd52/" class="sidebar-link">Promise 对象</a></li><li><a href="/blog/pages/48df907ad3570f3d/" class="sidebar-link">Iterator 和 for-of 循环</a></li><li><a href="/blog/pages/718b48ed9ce0adce/" class="sidebar-link">Generator 函数的语法</a></li><li><a href="/blog/pages/75af7031eb66847b/" class="sidebar-link">Generator 函数的异步应用</a></li><li><a href="/blog/pages/3777253e65bac487/" class="sidebar-link">async 函数</a></li><li><a href="/blog/pages/e831e1593c82bbe0/" class="sidebar-link">Class 的基本语法</a></li><li><a href="/blog/pages/83f8c3a0cd87dd83/" class="sidebar-link">Class 的继承</a></li><li><a href="/blog/pages/efe2fb04eb8ac5fb/" class="sidebar-link">Module 的语法</a></li><li><a href="/blog/pages/a79ca2e64ceae213/" class="sidebar-link">Module 的加载实现</a></li><li><a href="/blog/pages/984bf549204bb266/" class="sidebar-link">编程风格</a></li><li><a href="/blog/pages/32c35f7651d6e58e/" class="sidebar-link">读懂 ECMAScript 规格</a></li><li><a href="/blog/pages/16121351be68691b/" class="sidebar-link">异步遍历器</a></li><li><a href="/blog/pages/a2ba314746bfdbdd/" class="sidebar-link">ArrayBuffer</a></li><li><a href="/blog/pages/7188882b8d65af1b/" class="sidebar-link">最新提案</a></li><li><a href="/blog/pages/e97bc1e5626b082c/" class="sidebar-link">装饰器</a></li><li><a href="/blog/pages/1cf50330655efc69/" class="sidebar-link">函数式编程</a></li><li><a href="/blog/pages/6a8e2dc558da1b39/" class="sidebar-link">Mixin</a></li><li><a href="/blog/pages/8e8f80f69b775a56/" class="sidebar-link">SIMD</a></li><li><a href="/blog/pages/ea6f3b870f6dab69/" class="sidebar-link">参考链接</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-0c557b5e><div class="articleInfo" data-v-0c557b5e><ul class="breadcrumbs" data-v-0c557b5e><li data-v-0c557b5e><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-0c557b5e></a></li> <li data-v-0c557b5e><a href="/blog/note/es6/#《ES6 教程》笔记" data-v-0c557b5e>《ES6 教程》笔记</a></li></ul> <div class="info" data-v-0c557b5e><div title="作者" class="author iconfont icon-touxiang" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>阮一峰</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>2020-02-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">正则的扩展<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="正则的扩展"><a href="#正则的扩展" class="header-anchor">#</a> 正则的扩展</h1> <h2 id="regexp-构造函数"><a href="#regexp-构造函数" class="header-anchor">#</a> RegExp 构造函数</h2> <p>在 ES5 中，<code>RegExp</code>构造函数的参数有两种情况。</p> <p>第一种情况是，参数是字符串，这时第二个参数表示正则表达式的修饰符（flag）。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'xyz'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 等价于</span>
<span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div> <p>第二种情况是，参数是一个正则表示式，这时会返回一个原有正则表达式的拷贝。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 等价于</span>
<span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>但是，ES5 不允许此时使用第二个参数添加修饰符，否则会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Uncaught TypeError: Cannot supply flags when constructing one RegExp from another</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>ES6 改变了这种行为。如果<code>RegExp</code>构造函数第一个参数是一个正则对象，那么可以使用第二个参数指定修饰符。而且，返回的正则表达式会忽略原有的正则表达式的修饰符，只使用新指定的修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>flags
<span class="token comment">// &quot;i&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，原有正则对象的修饰符是<code>ig</code>，它会被第二个参数<code>i</code>覆盖。</p> <h2 id="字符串的正则方法"><a href="#字符串的正则方法" class="header-anchor">#</a> 字符串的正则方法</h2> <p>字符串对象共有 4 个方法，可以使用正则表达式：<code>match()</code>、<code>replace()</code>、<code>search()</code>和<code>split()</code>。</p> <p>ES6 将这 4 个方法，在语言内部全部调用<code>RegExp</code>的实例方法，从而做到所有与正则相关的方法，全都定义在<code>RegExp</code>对象上。</p> <ul><li><code>String.prototype.match</code> 调用 <code>RegExp.prototype[Symbol.match]</code></li> <li><code>String.prototype.replace</code> 调用 <code>RegExp.prototype[Symbol.replace]</code></li> <li><code>String.prototype.search</code> 调用 <code>RegExp.prototype[Symbol.search]</code></li> <li><code>String.prototype.split</code> 调用 <code>RegExp.prototype[Symbol.split]</code></li></ul> <h2 id="u-修饰符"><a href="#u-修饰符" class="header-anchor">#</a> u 修饰符</h2> <p>ES6 对正则表达式添加了<code>u</code>修饰符，含义为“Unicode 模式”，用来正确处理大于<code>\uFFFF</code>的 Unicode 字符。也就是说，会正确处理四个字节的 UTF-16 编码。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\uD83D</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\uD83D\uDC2A'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span><span class="token operator">^</span>\uD83D<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\uD83D\uDC2A'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>\uD83D\uDC2A</code>是一个四个字节的 UTF-16 编码，代表一个字符。但是，ES5 不支持四个字节的 UTF-16 编码，会将其识别为两个字符，导致第二行代码结果为<code>true</code>。加了<code>u</code>修饰符以后，ES6 就会识别其为一个字符，所以第一行代码结果为<code>false</code>。</p> <p>一旦加上<code>u</code>修饰符号，就会修改下面这些正则表达式的行为。</p> <p><strong>（1）点字符</strong></p> <p>点（<code>.</code>）字符在正则表达式中，含义是除了换行符以外的任意单个字符。对于码点大于<code>0xFFFF</code>的 Unicode 字符，点字符不能识别，必须加上<code>u</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'𠮷'</span><span class="token punctuation">;</span>

<span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^.$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span><span class="token operator">^</span><span class="token punctuation">.</span>$<span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码表示，如果不添加<code>u</code>修饰符，正则表达式就会认为字符串为两个字符，从而匹配失败。</p> <p><strong>（2）Unicode 字符表示法</strong></p> <p>ES6 新增了使用大括号表示 Unicode 字符，这种表示法在正则表达式中必须加上<code>u</code>修饰符，才能识别当中的大括号，否则会被解读为量词。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\u{61}</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span>\u<span class="token punctuation">{</span><span class="token number">61</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token operator">/</span>\u<span class="token punctuation">{</span>20BB7<span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码表示，如果不加<code>u</code>修饰符，正则表达式无法识别<code>\u{61}</code>这种表示法，只会认为这匹配 61 个连续的<code>u</code>。</p> <p><strong>（3）量词</strong></p> <p>使用<code>u</code>修饰符后，所有量词都会正确识别码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a{2}</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'aa'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token operator">/</span>a<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'aa'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token operator">/</span>𠮷<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span>𠮷<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>（4）预定义模式</strong></p> <p><code>u</code>修饰符也影响到预定义模式，能否正确识别码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\S$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span><span class="token operator">^</span>\<span class="token constant">S</span>$<span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码的<code>\S</code>是预定义模式，匹配所有非空白字符。只有加了<code>u</code>修饰符，它才能正确匹配码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <p>利用这一点，可以写出一个正确返回字符串长度的函数。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">codePointLength</span><span class="token punctuation">(</span><span class="token parameter">text</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> result <span class="token operator">=</span> text<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\s\S]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gu</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> result <span class="token operator">?</span> result<span class="token punctuation">.</span>length <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'𠮷𠮷'</span><span class="token punctuation">;</span>

s<span class="token punctuation">.</span>length <span class="token comment">// 4</span>
<span class="token function">codePointLength</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// 2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>（5）i 修饰符</strong></p> <p>有些 Unicode 字符的编码不同，但是字型很相近，比如，<code>\u004B</code>与<code>\u212A</code>都是大写的<code>K</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-z]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\u212A'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
<span class="token operator">/</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">/</span>iu<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\u212A'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，不加<code>u</code>修饰符，就无法识别非规范的<code>K</code>字符。</p> <p><strong>（6）转义</strong></p> <p>没有<code>u</code>修饰符的情况下，正则中没有定义的转义（如逗号的转义<code>\,</code>）无效，而在<code>u</code>模式会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\,</span><span class="token regex-delimiter">/</span></span> <span class="token comment">// /\,/</span>
<span class="token operator">/</span>\<span class="token punctuation">,</span><span class="token operator">/</span>u <span class="token comment">// 报错</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，没有<code>u</code>修饰符时，逗号前面的反斜杠是无效的，加了<code>u</code>修饰符就报错。</p> <h2 id="regexp-prototype-unicode-属性"><a href="#regexp-prototype-unicode-属性" class="header-anchor">#</a> RegExp.prototype.unicode 属性</h2> <p>正则实例对象新增<code>unicode</code>属性，表示是否设置了<code>u</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token keyword">const</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>

r1<span class="token punctuation">.</span>unicode <span class="token comment">// false</span>
r2<span class="token punctuation">.</span>unicode <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中，正则表达式是否设置了<code>u</code>修饰符，可以从<code>unicode</code>属性看出来。</p> <h2 id="y-修饰符"><a href="#y-修饰符" class="header-anchor">#</a> y 修饰符</h2> <p>除了<code>u</code>修饰符，ES6 还为正则表达式添加了<code>y</code>修饰符，叫做“粘连”（sticky）修饰符。</p> <p><code>y</code>修饰符的作用与<code>g</code>修饰符类似，也是全局匹配，后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于，<code>g</code>修饰符只要剩余位置中存在匹配就可，而<code>y</code>修饰符确保匹配必须从剩余的第一个位置开始，这也就是“粘连”的涵义。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa_aa_a'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>

r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;aaa&quot;]</span>
r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;aaa&quot;]</span>

r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;aa&quot;]</span>
r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// null</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码有两个正则表达式，一个使用<code>g</code>修饰符，另一个使用<code>y</code>修饰符。这两个正则表达式各执行了两次，第一次执行的时候，两者行为相同，剩余字符串都是<code>_aa_a</code>。由于<code>g</code>修饰没有位置要求，所以第二次执行会返回结果，而<code>y</code>修饰符要求匹配必须从头部开始，所以返回<code>null</code>。</p> <p>如果改一下正则表达式，保证每次都能头部匹配，<code>y</code>修饰符就会返回结果了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa_aa_a'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+_</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>

r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;aaa_&quot;]</span>
r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;aa_&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码每次匹配，都是从剩余字符串的头部开始。</p> <p>使用<code>lastIndex</code>属性，可以更好地说明<code>y</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>

<span class="token comment">// 指定从2号位置（y）开始匹配</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>

<span class="token comment">// 匹配成功</span>
<span class="token keyword">const</span> match <span class="token operator">=</span> <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在3号位置匹配成功</span>
match<span class="token punctuation">.</span>index <span class="token comment">// 3</span>

<span class="token comment">// 下一次匹配从4号位开始</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token comment">// 4</span>

<span class="token comment">// 4号位开始匹配失败</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中，<code>lastIndex</code>属性指定每次搜索的开始位置，<code>g</code>修饰符从这个位置开始向后搜索，直到发现匹配为止。</p> <p><code>y</code>修饰符同样遵守<code>lastIndex</code>属性，但是要求必须在<code>lastIndex</code>指定的位置发现匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>

<span class="token comment">// 指定从2号位置开始匹配</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>

<span class="token comment">// 不是粘连，匹配失败</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>

<span class="token comment">// 指定从3号位置开始匹配</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>

<span class="token comment">// 3号位置是粘连，匹配成功</span>
<span class="token keyword">const</span> match <span class="token operator">=</span> <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
match<span class="token punctuation">.</span>index <span class="token comment">// 3</span>
<span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token comment">// 4</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>实际上，<code>y</code>修饰符号隐含了头部匹配的标志<code>^</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'aba'</span><span class="token punctuation">)</span>
<span class="token comment">// null</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码由于不能保证头部匹配，所以返回<code>null</code>。<code>y</code>修饰符的设计本意，就是让头部匹配的标志<code>^</code>在全局匹配中都有效。</p> <p>下面是字符串对象的<code>replace</code>方法的例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gy</span></span><span class="token punctuation">;</span>
<span class="token string">'aaxa'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token constant">REGEX</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span> <span class="token comment">// '--xa'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，最后一个<code>a</code>因为不是出现在下一次匹配的头部，所以不会被替换。</p> <p>单单一个<code>y</code>修饰符对<code>match</code>方法，只能返回第一个匹配，必须与<code>g</code>修饰符联用，才能返回所有匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token string">'a1a2a3'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a1&quot;]</span>
<span class="token string">'a1a2a3'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gy</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a1&quot;, &quot;a2&quot;, &quot;a3&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>y</code>修饰符的一个应用，是从字符串提取 token（词元），<code>y</code>修饰符确保了匹配之间不会有漏掉的字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">TOKEN_Y</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s*(\+|[0-9]+)\s*</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">TOKEN_G</span>  <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s*(\+|[0-9]+)\s*</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>

<span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_Y</span><span class="token punctuation">,</span> <span class="token string">'3 + 4'</span><span class="token punctuation">)</span>
<span class="token comment">// [ '3', '+', '4' ]</span>
<span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_G</span><span class="token punctuation">,</span> <span class="token string">'3 + 4'</span><span class="token punctuation">)</span>
<span class="token comment">// [ '3', '+', '4' ]</span>

<span class="token keyword">function</span> <span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token parameter"><span class="token constant">TOKEN_REGEX</span><span class="token punctuation">,</span> str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> match<span class="token punctuation">;</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>match <span class="token operator">=</span> <span class="token constant">TOKEN_REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    result<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>match<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中，如果字符串里面没有非法字符，<code>y</code>修饰符与<code>g</code>修饰符的提取结果是一样的。但是，一旦出现非法字符，两者的行为就不一样了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_Y</span><span class="token punctuation">,</span> <span class="token string">'3x + 4'</span><span class="token punctuation">)</span>
<span class="token comment">// [ '3' ]</span>
<span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_G</span><span class="token punctuation">,</span> <span class="token string">'3x + 4'</span><span class="token punctuation">)</span>
<span class="token comment">// [ '3', '+', '4' ]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中，<code>g</code>修饰符会忽略非法字符，而<code>y</code>修饰符不会，这样就很容易发现错误。</p> <h2 id="regexp-prototype-sticky-属性"><a href="#regexp-prototype-sticky-属性" class="header-anchor">#</a> RegExp.prototype.sticky 属性</h2> <p>与<code>y</code>修饰符相匹配，ES6 的正则实例对象多了<code>sticky</code>属性，表示是否设置了<code>y</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
r<span class="token punctuation">.</span>sticky <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="regexp-prototype-flags-属性"><a href="#regexp-prototype-flags-属性" class="header-anchor">#</a> RegExp.prototype.flags 属性</h2> <p>ES6 为正则表达式新增了<code>flags</code>属性，会返回正则表达式的修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// ES5 的 source 属性</span>
<span class="token comment">// 返回正则表达式的正文</span>
<span class="token operator">/</span>abc<span class="token operator">/</span>ig<span class="token punctuation">.</span>source
<span class="token comment">// &quot;abc&quot;</span>

<span class="token comment">// ES6 的 flags 属性</span>
<span class="token comment">// 返回正则表达式的修饰符</span>
<span class="token operator">/</span>abc<span class="token operator">/</span>ig<span class="token punctuation">.</span>flags
<span class="token comment">// 'gi'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="s-修饰符-dotall-模式"><a href="#s-修饰符-dotall-模式" class="header-anchor">#</a> s 修饰符：dotAll 模式</h2> <p>正则表达式中，点（<code>.</code>）是一个特殊字符，代表任意的单个字符，但是有两个例外。一个是四个字节的 UTF-16 字符，这个可以用<code>u</code>修饰符解决；另一个是行终止符（line terminator character）。</p> <p>所谓行终止符，就是该字符表示一行的终结。以下四个字符属于“行终止符”。</p> <ul><li>U+000A 换行符（<code>\n</code>）</li> <li>U+000D 回车符（<code>\r</code>）</li> <li>U+2028 行分隔符（line separator）</li> <li>U+2029 段分隔符（paragraph separator）</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span>
<span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，因为<code>.</code>不匹配<code>\n</code>，所以正则表达式返回<code>false</code>。</p> <p>但是，很多时候我们希望匹配的是任意单个字符，这时有一种变通的写法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo[^]bar</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span>
<span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这种解决方案毕竟不太符合直觉，ES2018 <a href="https://github.com/tc39/proposal-regexp-dotall-flag" target="_blank" rel="noopener noreferrer">引入<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><code>s</code>修饰符，使得<code>.</code>可以匹配任意单个字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span><span class="token regex-flags">s</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这被称为<code>dotAll</code>模式，即点（dot）代表一切字符。所以，正则表达式还引入了一个<code>dotAll</code>属性，返回一个布尔值，表示该正则表达式是否处在<code>dotAll</code>模式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span><span class="token regex-flags">s</span></span><span class="token punctuation">;</span>
<span class="token comment">// 另一种写法</span>
<span class="token comment">// const re = new RegExp('foo.bar', 's');</span>

re<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
re<span class="token punctuation">.</span>dotAll <span class="token comment">// true</span>
re<span class="token punctuation">.</span>flags <span class="token comment">// 's'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>/s</code>修饰符和多行修饰符<code>/m</code>不冲突，两者一起使用的情况下，<code>.</code>匹配所有字符，而<code>^</code>和<code>$</code>匹配每一行的行首和行尾。</p> <h2 id="后行断言"><a href="#后行断言" class="header-anchor">#</a> 后行断言</h2> <p>JavaScript 语言的正则表达式，只支持先行断言（lookahead）和先行否定断言（negative lookahead），不支持后行断言（lookbehind）和后行否定断言（negative lookbehind）。ES2018 引入<a href="https://github.com/tc39/proposal-regexp-lookbehind" target="_blank" rel="noopener noreferrer">后行断言<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，V8 引擎 4.9 版（Chrome 62）已经支持。</p> <p>“先行断言”指的是，<code>x</code>只有在<code>y</code>前面才匹配，必须写成<code>/x(?=y)/</code>。比如，只匹配百分号之前的数字，要写成<code>/\d+(?=%)/</code>。“先行否定断言”指的是，<code>x</code>只有不在<code>y</code>前面才匹配，必须写成<code>/x(?!y)/</code>。比如，只匹配不在百分号之前的数字，要写成<code>/\d+(?!%)/</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d+(?=%)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'100% of US presidents have been male'</span><span class="token punctuation">)</span>  <span class="token comment">// [&quot;100&quot;]</span>
<span class="token operator">/</span>\d<span class="token operator">+</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">!</span><span class="token operator">%</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'that’s all 44 of them'</span><span class="token punctuation">)</span>                 <span class="token comment">// [&quot;44&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面两个字符串，如果互换正则表达式，就不会得到相同结果。另外，还可以看到，“先行断言”括号之中的部分（<code>(?=%)</code>），是不计入返回结果的。</p> <p>“后行断言”正好与“先行断言”相反，<code>x</code>只有在<code>y</code>后面才匹配，必须写成<code>/(?&lt;=y)x/</code>。比如，只匹配美元符号之后的数字，要写成<code>/(?&lt;=\$)\d+/</code>。“后行否定断言”则与“先行否定断言”相反，<code>x</code>只有不在<code>y</code>后面才匹配，必须写成<code>/(?&lt;!y)x/</code>。比如，只匹配不在美元符号后面的数字，要写成<code>/(?&lt;!\$)\d+/</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;=\$)\d+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'Benjamin Franklin is on the $100 bill'</span><span class="token punctuation">)</span>  <span class="token comment">// [&quot;100&quot;]</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;</span><span class="token operator">!</span>\$<span class="token punctuation">)</span>\d<span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'it’s is worth about €90'</span><span class="token punctuation">)</span>                <span class="token comment">// [&quot;90&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的例子中，“后行断言”的括号之中的部分（<code>(?&lt;=\$)</code>），也是不计入返回结果。</p> <p>下面的例子是使用后行断言进行字符串替换。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DOLLAR_PREFIX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;=\$)foo</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token string">'$foo %foo foo'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token constant">RE_DOLLAR_PREFIX</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// '$bar %foo foo'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，只有在美元符号后面的<code>foo</code>才会被替换。</p> <p>“后行断言”的实现，需要先匹配<code>/(?&lt;=y)x/</code>的<code>x</code>，然后再回到左边，匹配<code>y</code>的部分。这种“先右后左”的执行顺序，与所有其他正则操作相反，导致了一些不符合预期的行为。</p> <p>首先，后行断言的组匹配，与正常情况下结果是不一样的。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;=(\d+)(\d+))$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1053'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;&quot;, &quot;1&quot;, &quot;053&quot;]</span>
<span class="token operator">/</span><span class="token operator">^</span><span class="token punctuation">(</span>\d<span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">(</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>$<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1053'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;1053&quot;, &quot;105&quot;, &quot;3&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，需要捕捉两个组匹配。没有“后行断言”时，第一个括号是贪婪模式，第二个括号只能捕获一个字符，所以结果是<code>105</code>和<code>3</code>。而“后行断言”时，由于执行顺序是从右到左，第二个括号是贪婪模式，第一个括号只能捕获一个字符，所以结果是<code>1</code>和<code>053</code>。</p> <p>其次，“后行断言”的反斜杠引用，也与通常的顺序相反，必须放在对应的那个括号之前。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;=(o)d\1)r</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'hodor'</span><span class="token punctuation">)</span>  <span class="token comment">// null</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;=</span>\<span class="token number">1</span><span class="token function">d</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">)</span>r<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'hodor'</span><span class="token punctuation">)</span>  <span class="token comment">// [&quot;r&quot;, &quot;o&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，如果后行断言的反斜杠引用（<code>\1</code>）放在括号的后面，就不会得到匹配结果，必须放在前面才可以。因为后行断言是先从左到右扫描，发现匹配以后再回过头，从右到左完成反斜杠引用。</p> <h2 id="unicode-属性类"><a href="#unicode-属性类" class="header-anchor">#</a> Unicode 属性类</h2> <p>ES2018 <a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes" target="_blank" rel="noopener noreferrer">引入<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>了一种新的类的写法<code>\p{...}</code>和<code>\P{...}</code>，允许正则表达式匹配符合 Unicode 某种属性的所有字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> regexGreekSymbol <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\p{Script=Greek}</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
regexGreekSymbol<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'π'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>\p{Script=Greek}</code>指定匹配一个希腊文字母，所以匹配<code>π</code>成功。</p> <p>Unicode 属性类要指定属性名和属性值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>\p<span class="token punctuation">{</span>UnicodePropertyName<span class="token operator">=</span>UnicodePropertyValue<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>对于某些属性，可以只写属性名，或者只写属性值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>\p<span class="token punctuation">{</span>UnicodePropertyName<span class="token punctuation">}</span>
\p<span class="token punctuation">{</span>UnicodePropertyValue<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>\P{…}</code>是<code>\p{…}</code>的反向匹配，即匹配不满足条件的字符。</p> <p>注意，这两种类只对 Unicode 有效，所以使用的时候一定要加上<code>u</code>修饰符。如果不加<code>u</code>修饰符，正则表达式使用<code>\p</code>和<code>\P</code>会报错，ECMAScript 预留了这两个类。</p> <p>由于 Unicode 的各种属性非常多，所以这种新的类的表达能力非常强。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Decimal_Number}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𝟏𝟐𝟑𝟜𝟝𝟞𝟩𝟪𝟫𝟬𝟭𝟮𝟯𝟺𝟻𝟼'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，属性类指定匹配所有十进制字符，可以看到各种字型的十进制字符都会匹配成功。</p> <p><code>\p{Number}</code>甚至能匹配罗马数字。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 匹配所有数字</span>
<span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Number}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'²³¹¼½¾'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'㉛㉜㉝'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>下面是其他一些例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 匹配所有空格</span>
\p<span class="token punctuation">{</span>White_Space<span class="token punctuation">}</span>

<span class="token comment">// 匹配各种文字的所有字母，等同于 Unicode 版的 \w</span>
<span class="token punctuation">[</span>\p<span class="token punctuation">{</span>Alphabetic<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Mark<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Decimal_Number<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Connector_Punctuation<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Join_Control<span class="token punctuation">}</span><span class="token punctuation">]</span>

<span class="token comment">// 匹配各种文字的所有非字母的字符，等同于 Unicode 版的 \W</span>
<span class="token punctuation">[</span><span class="token operator">^</span>\p<span class="token punctuation">{</span>Alphabetic<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Mark<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Decimal_Number<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Connector_Punctuation<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Join_Control<span class="token punctuation">}</span><span class="token punctuation">]</span>

<span class="token comment">// 匹配 Emoji</span>
<span class="token operator">/</span>\p<span class="token punctuation">{</span>Emoji_Modifier_Base<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Emoji_Modifier<span class="token punctuation">}</span><span class="token operator">?</span><span class="token operator">|</span>\p<span class="token punctuation">{</span>Emoji_Presentation<span class="token punctuation">}</span><span class="token operator">|</span>\p<span class="token punctuation">{</span>Emoji<span class="token punctuation">}</span>\uFE0F<span class="token operator">/</span>gu

<span class="token comment">// 匹配所有的箭头字符</span>
<span class="token keyword">const</span> regexArrows <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Block=Arrows}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
regexArrows<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'←↑→↓↔↕↖↗↘↙⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇧⇩'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="具名组匹配"><a href="#具名组匹配" class="header-anchor">#</a> 具名组匹配</h2> <h3 id="简介"><a href="#简介" class="header-anchor">#</a> 简介</h3> <p>正则表达式使用圆括号进行组匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\d{4})-(\d{2})-(\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中，正则表达式里面有三组圆括号。使用<code>exec</code>方法，就可以将这三组匹配结果提取出来。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\d{4})-(\d{2})-(\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>

<span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_DATE</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1999-12-31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> year <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 1999</span>
<span class="token keyword">const</span> month <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 12</span>
<span class="token keyword">const</span> day <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 31</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>组匹配的一个问题是，每一组的匹配含义不容易看出来，而且只能用数字序号（比如<code>matchObj[1]</code>）引用，要是组的顺序变了，引用的时候就必须修改序号。</p> <p>ES2018 引入了<a href="https://github.com/tc39/proposal-regexp-named-groups" target="_blank" rel="noopener noreferrer">具名组匹配<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>（Named Capture Groups），允许为每一个组匹配指定一个名字，既便于阅读代码，又便于引用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>

<span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_DATE</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1999-12-31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> year <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>year<span class="token punctuation">;</span> <span class="token comment">// 1999</span>
<span class="token keyword">const</span> month <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>month<span class="token punctuation">;</span> <span class="token comment">// 12</span>
<span class="token keyword">const</span> day <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>day<span class="token punctuation">;</span> <span class="token comment">// 31</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中，“具名组匹配”在圆括号内部，模式的头部添加“问号 + 尖括号 + 组名”（<code>?&lt;year&gt;</code>），然后就可以在<code>exec</code>方法返回结果的<code>groups</code>属性上引用该组名。同时，数字序号（<code>matchObj[1]</code>）依然有效。</p> <p>具名组匹配等于为每一组匹配加上了 ID，便于描述匹配的目的。如果组的顺序变了，也不用改变匹配后的处理代码。</p> <p>如果具名组没有匹配，那么对应的<code>groups</code>对象属性会是<code>undefined</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_OPT_A</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?&lt;as&gt;a+)?$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_OPT_A</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>as <span class="token comment">// undefined</span>
<span class="token string">'as'</span> <span class="token keyword">in</span> matchObj<span class="token punctuation">.</span>groups <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中，具名组<code>as</code>没有找到匹配，那么<code>matchObj.groups.as</code>属性值就是<code>undefined</code>，并且<code>as</code>这个键名在<code>groups</code>是始终存在的。</p> <h3 id="解构赋值和替换"><a href="#解构赋值和替换" class="header-anchor">#</a> 解构赋值和替换</h3> <p>有了具名组匹配以后，可以使用解构赋值直接从匹配结果上为变量赋值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token punctuation">{</span><span class="token literal-property property">groups</span><span class="token operator">:</span> <span class="token punctuation">{</span>one<span class="token punctuation">,</span> two<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?&lt;one&gt;.*):(?&lt;two&gt;.*)$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'foo:bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
one  <span class="token comment">// foo</span>
two  <span class="token comment">// bar</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>字符串替换时，使用<code>$&lt;组名&gt;</code>引用具名组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>

<span class="token string">'2015-01-02'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">'$&lt;day&gt;/$&lt;month&gt;/$&lt;year&gt;'</span><span class="token punctuation">)</span>
<span class="token comment">// '02/01/2015'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中，<code>replace</code>方法的第二个参数是一个字符串，而不是正则表达式。</p> <p><code>replace</code>方法的第二个参数也可以是函数，该函数的参数序列如下。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token string">'2015-01-02'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token punctuation">(</span>
   matched<span class="token punctuation">,</span> <span class="token comment">// 整个匹配结果 2015-01-02</span>
   capture1<span class="token punctuation">,</span> <span class="token comment">// 第一个组匹配 2015</span>
   capture2<span class="token punctuation">,</span> <span class="token comment">// 第二个组匹配 01</span>
   capture3<span class="token punctuation">,</span> <span class="token comment">// 第三个组匹配 02</span>
   position<span class="token punctuation">,</span> <span class="token comment">// 匹配开始的位置 0</span>
   <span class="token constant">S</span><span class="token punctuation">,</span> <span class="token comment">// 原字符串 2015-01-02</span>
   groups <span class="token comment">// 具名组构成的一个对象 {year, month, day}</span>
 <span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
 <span class="token keyword">let</span> <span class="token punctuation">{</span>day<span class="token punctuation">,</span> month<span class="token punctuation">,</span> year<span class="token punctuation">}</span> <span class="token operator">=</span> groups<span class="token punctuation">;</span>
 <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>day<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>month<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>year<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>具名组匹配在原来的基础上，新增了最后一个函数参数：具名组构成的一个对象。函数内部可以直接对这个对象进行解构赋值。</p> <h3 id="引用"><a href="#引用" class="header-anchor">#</a> 引用</h3> <p>如果要在正则表达式内部引用某个“具名组匹配”，可以使用<code>\k&lt;组名&gt;</code>的写法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?&lt;word&gt;[a-z]+)!\k&lt;word&gt;$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>数字引用（<code>\1</code>）依然有效。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?&lt;word&gt;[a-z]+)!\1$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这两种引用语法还可以同时使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?&lt;word&gt;[a-z]+)!\k&lt;word&gt;!\1$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="string-prototype-matchall"><a href="#string-prototype-matchall" class="header-anchor">#</a> String.prototype.matchAll()</h2> <p>如果一个正则表达式在字符串里面有多个匹配，现在一般使用<code>g</code>修饰符或<code>y</code>修饰符，在循环里面逐一取出。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">t(e)(st(\d?))</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> string <span class="token operator">=</span> <span class="token string">'test1test2test3'</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> matches <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> match<span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>match <span class="token operator">=</span> regex<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  matches<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

matches
<span class="token comment">// [</span>
<span class="token comment">//   [&quot;test1&quot;, &quot;e&quot;, &quot;st1&quot;, &quot;1&quot;, index: 0, input: &quot;test1test2test3&quot;],</span>
<span class="token comment">//   [&quot;test2&quot;, &quot;e&quot;, &quot;st2&quot;, &quot;2&quot;, index: 5, input: &quot;test1test2test3&quot;],</span>
<span class="token comment">//   [&quot;test3&quot;, &quot;e&quot;, &quot;st3&quot;, &quot;3&quot;, index: 10, input: &quot;test1test2test3&quot;]</span>
<span class="token comment">// ]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码中，<code>while</code>循环取出每一轮的正则匹配，一共三轮。</p> <p><a href="https://github.com/tc39/proposal-string-matchall" target="_blank" rel="noopener noreferrer">ES2020<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 增加了<code>String.prototype.matchAll()</code>方法，可以一次性取出所有匹配。不过，它返回的是一个遍历器（Iterator），而不是数组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> string <span class="token operator">=</span> <span class="token string">'test1test2test3'</span><span class="token punctuation">;</span>

<span class="token comment">// g 修饰符加不加都可以</span>
<span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">t(e)(st(\d?))</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> match <span class="token keyword">of</span> string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// [&quot;test1&quot;, &quot;e&quot;, &quot;st1&quot;, &quot;1&quot;, index: 0, input: &quot;test1test2test3&quot;]</span>
<span class="token comment">// [&quot;test2&quot;, &quot;e&quot;, &quot;st2&quot;, &quot;2&quot;, index: 5, input: &quot;test1test2test3&quot;]</span>
<span class="token comment">// [&quot;test3&quot;, &quot;e&quot;, &quot;st3&quot;, &quot;3&quot;, index: 10, input: &quot;test1test2test3&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中，由于<code>string.matchAll(regex)</code>返回的是遍历器，所以可以用<code>for...of</code>循环取出。相对于返回数组，返回遍历器的好处在于，如果匹配结果是一个很大的数组，那么遍历器比较节省资源。</p> <p>遍历器转为数组是非常简单的，使用<code>...</code>运算符和<code>Array.from()</code>方法就可以了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 转为数组方法一</span>
<span class="token punctuation">[</span><span class="token operator">...</span>string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">]</span>

<span class="token comment">// 转为数组方法二</span>
Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div></div>  <div class="page-edit"><div class="edit-link"><a href="https://github.com/xugaoyi/vuepress-theme-vdoing/edit/master/docs/《ES6 教程》笔记/06.正则的扩展.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=ES6" title="标签">#ES6</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/01/18, 12:59:48</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/a650b4a0ebfc9350/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">字符串的新增方法</div></a> <a href="/blog/pages/5dfea9a0f2d1a392/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">数值的扩展</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/blog/pages/a650b4a0ebfc9350/" class="prev">字符串的新增方法</a></span> <span class="next"><a href="/blog/pages/5dfea9a0f2d1a392/">数值的扩展</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/dcebaf/"><div>
            一行代码“黑”掉任意网站
            <span class="title-tag">
              原创
            </span></div></a> <span class="date">11-25</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/a61298/"><div>
            33个非常实用的JavaScript一行代码
            <!----></div></a> <span class="date">11-02</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/82baa3/"><div>
            使用State Hook
            <!----></div></a> <span class="date">04-06</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:894072666@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/xugaoyi" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2019-2022
    <span>Evan Xu | <a href="https://github.com/xugaoyi/vuepress-theme-vdoing/blob/master/LICENSE" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/blog/assets/js/app.aaf1b95a.js" defer></script><script src="/blog/assets/js/2.4e88da26.js" defer></script><script src="/blog/assets/js/89.a7a762a6.js" defer></script>
  </body>
</html>
